import request from "@/utils";
import axios, { CancelTokenSource } from "axios";

type Response<T> = {
    code: string,
    message: string,
    data: T
}

type Login = {
    token: string,
    userid: string
}
export function login(loginname: string, password: string) {
    return request.post<any, { data: Response<Login> }>('/user/login', {
        loginname,
        password
    })
}

type Sms = {
    code: string,
    message: string,
    data: number
}
export function telCheck(tel: string) {
    return request.post<any, { data: Response<null> }>('user/docheckphone', {
        tel
    })
}
export function smsRequst(tel: string) {
    return request.post<any, { data: Response<Sms> }>('user/dosendmsgcode', {
        tel
    })
}

type SmsCheck = {
    code: string,
    message: string
}
export function smsCheck(tel: string, telcode: string) {
    return request.post<any, { data: Response<SmsCheck> }>('user/docheckcode', {
        tel,
        telcode
    })
}

type Register = {
    code: string,
    message: string
}
export function register(tel: string, password: string) {
    return request.post<any, { data: Response<Register> }>('user/dofinishregister', {
        tel,
        password
    })
}

type Banner = {
    img: string,
    alt: string,
    flag: boolean,
    link: string,
    bannerid: string

}
export function bannerList() {
    return request.get<any, { data: Response<Banner[]> }>('banner/list')
}

type TimeList = {
    banners: string[],
    desc: string,
    originprice: number,
    category: string,
    brand: string,
    proname: string,
    proid: string,
    img1: string
}
export function timeList(count: number, limitNum: number) {
    let url = 'pro/seckilllist';
    url += `?count=${count}&limitNum=${limitNum}`;
    return request.get<any, { data: Response<TimeList[]> }>(url);
}


export function homeList(count: number, limitNum: number) {
    let url = 'pro/list';
    url += `?count=${count}&limitNum=${limitNum}`;
    return request.get<any, { data: Response<TimeList[]> }>(url);
}

export function getProduct(proid: string) {
    let url = 'pro/detail';
    url += `/${proid}`;
    return request.get<any, { data: Response<TimeList> }>(url);
}

export function addCar(proid: string, num: string) {
    return request.post<any, { data: Response<null> }>('cart/add', {
        userid: localStorage.getItem('userid'),
        proid,
        num
    },
        {
            headers: {
                'token': localStorage.getItem('token')
            }
        }
    )
}
type Card = {
    cartid: string
    discount: number
    flag: boolean
    img1: string
    num: number
    originprice: number
    proid: string
    proname: string
}
export function card() {
    return request.post<any, { data: Response<Card> }>('cart/list', {
        userid: localStorage.getItem('userid')
    },
        {
            headers: {
                'token': localStorage.getItem('token')
            }
        }
    )
}

let changeNumSource: CancelTokenSource;
export function changeNum(cartid: string, num: number) {
    if (changeNumSource) {
        changeNumSource.cancel()
    }

    // 2. 生成 cancelToken
    changeNumSource = axios.CancelToken.source()
    return request.post<any, { data: Response<null> }>('cart/updatenum', {
        num,
        cartid
    },
        // 3. 传递 cancelToken 属性
        //  1） post 请求的第三个参数，传递一个属性 cancelToken
        //  2)  get  请求的第二个参数，传递一个属性 cancelToken
        {
            cancelToken: changeNumSource.token,
            headers: {
                'token': localStorage.getItem('token')
            }
        }

    )
}

export function removeProduct(cartid: string) {
    return request.post<any, { data: Response<null> }>('cart/remove', {
        cartid
    },
        {
            headers: {
                'token': localStorage.getItem('token')
            }
        }
    )
}

export function changeFlag(cartid: string, flag: boolean) {
    return request.post<any, { data: Response<null> }>('cart/selectone', {
        cartid,
        flag
    },
        {
            headers: {
                'token': localStorage.getItem('token')
            }
        }
    )
}
export function changeFlagAll(type: boolean) {
    return request.post<any, { data: Response<null> }>('cart/selectall', {
        userid: localStorage.getItem('userid'),
        type
    },
        {
            headers: {
                'token': localStorage.getItem('token')
            }
        }
    )
}

export function carMore(count: number, limitNum: number) {
    let url = 'pro/recommendlist';
    url += `?count=${count}&limitNum=${limitNum}`;
    return request.get<any, { data: Response<TimeList[]> }>(url);
}

export function search(count: number, limitNum: number, keyword: string) {
    let url = 'pro/search';
    url += `?count=${count}&limitNum=${limitNum}&keyword=${keyword}`;
    return request.get<any, { data: Response<TimeList[]> }>(url);
}
type Cities = {
    letter: string
    data: [
        {
            cityId: number,
            name: string,
            pinyin: string,
            isHot: number
        }
    ]
}
export function getCities() {
    return request.get<any, Response<Cities[]>>('city/sortCity.json')
}

export function removeAll() {
    return request.post<any, { data: Response<null> }>('cart/removeall', {
        userid: localStorage.getItem('userid')
    },
        {
            headers: {
                'token': localStorage.getItem('token')
            }
        }
    )
}






export function addOrder() {
    return request.post<any, { data: Response<null> }>('order/addOrder', {

    },
        {
            headers: {
                'token': localStorage.getItem('token')
            }
        }
    )
}